//
//  Level2TableViewCell.m
//  dealer
//
//  Created by  ibokan on 10-9-15.
//  Copyright 2010 tencent.com. All rights reserved.
//

#import "Level2TableViewCell.h"


@implementation Level2TableViewCell


@synthesize addImageView1,mapButton;
@synthesize phoneImageView1,phoneButton;
@synthesize dateImageView;
@synthesize delegate;
@synthesize index;
@synthesize height;


-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
	{
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 0, 0)];
		[label1 setBackgroundColor:[UIColor clearColor]];
		[label1 setFont:[UIFont systemFontOfSize:12]];
		[label1 setText:@"公司："];
		[label1 sizeToFit];
		[self.contentView addSubview:label1];
		[label1 release];
		
		
		companyLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 2, 170, 0)];
		companyLabel.backgroundColor = [UIColor clearColor];
		[companyLabel setLineBreakMode:UILineBreakModeWordWrap];
		companyLabel.numberOfLines = 0;
		companyLabel.font = [UIFont systemFontOfSize:12];
		[self.contentView addSubview:companyLabel];
		[companyLabel release];
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		label2 = [[UILabel alloc] init];
		[label2 setBackgroundColor:[UIColor clearColor]];
		[label2 setFont:[UIFont systemFontOfSize:12]];
		[label2 setText:@"电话："];
		[self.contentView addSubview:label2];
		[label2 release];
		
		
		phoneLabel = [[UILabel alloc] init];
		phoneLabel.backgroundColor = [UIColor clearColor];
		phoneLabel.font = [UIFont systemFontOfSize:12];
		[self.contentView addSubview:phoneLabel];
		[phoneLabel release];
				
		phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[phoneButton setBackgroundImage:[UIImage imageNamed:@"call.png"] forState:UIControlStateNormal];
		[phoneButton addTarget:self action:@selector(telephone) forControlEvents:UIControlEventTouchUpInside];
		[self.contentView addSubview:phoneButton];
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		label3 = [[UILabel alloc] init];
		[label3 setBackgroundColor:[UIColor clearColor]];
		[label3 setFont:[UIFont systemFontOfSize:12]];
		[label3 setText:@"地址："];
		[self.contentView addSubview:label3];
		[label3 release];
		
		
		addressLabel = [[UILabel alloc] init];
		addressLabel.backgroundColor = [UIColor clearColor];
		addressLabel.font = [UIFont systemFontOfSize:12];
		[addressLabel setLineBreakMode:UILineBreakModeWordWrap];
		[addressLabel setNumberOfLines:0];
		[self.contentView addSubview:addressLabel];
		[addressLabel release];
		
		
		mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[mapButton setBackgroundImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
		[mapButton addTarget:self action:@selector(showMap) forControlEvents:UIControlEventTouchUpInside];
		[self.contentView addSubview:mapButton];
		
		
	}
	
	return self;
}

- (void)setCompanyLabelText:(NSString *) companyName;
{
	companyLabel.text = companyName;
	[companyLabel sizeToFit];
}

- (void)setPhoneLabelText:(NSString *) phoneNumber{
	CGFloat phone_y = companyLabel.frame.size.height;
	label2.frame = CGRectMake(10, phone_y + 5, 0, 0);
	[label2 sizeToFit];
	
	phoneLabel.frame = CGRectMake(45, phone_y + 5, 0, 0);
	phoneLabel.text = phoneNumber;
	[phoneLabel sizeToFit];
	
		
	phoneButton.frame = CGRectMake(180, phone_y + 5, 17, 17);
	
	UIControl *control2 = [[UIControl alloc] initWithFrame:CGRectMake(165, phone_y - 5, 45, 45)];
	[control2 addTarget:self action:@selector(telephone) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:control2];
	
//	[self bringSubviewToFront:phoneButton];
	
	
	
	UIControl *control1 = [[UIControl alloc] initWithFrame:phoneLabel.frame];
	[control1 addTarget:self action:@selector(telephone) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:control1];
	[control1 release];
}

- (void)setAddressLabelText:(NSString *) address{
	CGFloat address_y = phoneLabel.frame.origin.y + phoneLabel.frame.size.height + 3;
	label3.frame = CGRectMake(10, address_y, 0, 0);
	[label3 sizeToFit];
	
	addressLabel.text = address;
	addressLabel.frame = CGRectMake(45, address_y, 190, 0);
	[addressLabel sizeToFit];
	
	height = addressLabel.frame.origin.y + addressLabel.frame.size.height + 5;
	
	mapButton.frame = CGRectMake(240, address_y , 17, 17);
	
	UIControl *control1 = [[UIControl alloc] initWithFrame:CGRectMake(225, address_y - 10, 50, 50)];
	[control1 addTarget:self action:@selector(showMap) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:control1];
	[control1 release];
	
	UIControl *control2 = [[UIControl alloc] initWithFrame:addressLabel.frame];
	[control2 addTarget:self action:@selector(showMap) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:control2];
	[control2 release];

}


-(void) showMap
{
	[delegate showMapButtonSelected:index];
}

-(void) telephone
{
	[delegate telephoneButtonSelected:index];
}


@end
